<?php
declare(strict_types=1);

namespace LowCode;

use LowCode\Abstracts\Core;
use LowCode\Traits\HasCall;

/**
 * @method static $this make(string $name, string $title)
 * @method $this id(string $id) 唯一标识
 * @method $this name(string $name) 文件名
 * @method $this title(string $title) 页面标题|板块标题
 * @method $this type(string $type = 'block'|'page') 文件名
 * @method $this fromType(string $type = 'Schema'|'UrlSchema'|'Plugin') 区块来源，默认为Schema，一旦确定，不允许更改
 * @method $this preset(bool $bool) 是否预设，预设的插件不能编辑和删除
 * @method $this urls(string $urls) 资源url，Schema: 无url, UrlSchema： 只允许一个json文件, Plugin：支持多个文件（.css或.js）, 多个文件用逗号分隔
 * @method $this library(string $library) Plugin 时的插件名称
 * @property string $id
 * @property string $name
 */
class BlockFile extends Core
{
    protected static array $allowAttributes = [
        "id","name","title","type","fromType","preset","urls","library","dsl"
    ];

    use HasCall;


    /**
     * @param string $name 文件名
     * @param string $title 页面标题|板块标题
     */
    public function __construct(string $name, string $title)
    {
        parent::__construct([
            "id"=>autoId("block"),
            "type"=>"block",
            "name"=>$name,
            "title"=>$title,
            "fromType"=>"Schema",
        ]);
    }

    /**
     * 来源 Schema dsl
     * @return $this
     */
    public function dsl(Block $dsl): static
    {
        $dsl->id($this->id);
        return $this->setAttribute("dsl", $dsl);
    }

    /**
     * 来源 Schema dsl
     * @return $this
     */
    public function fromSchema(Block $dsl): static
    {
        return $this->fromType("Schema")->dsl($dsl);
    }

    /**
     * 支持多个文件（.css或.js）
     * @param string $url json响应或者json文件路径
     * @return $this
     */
    public function fromUrl(string $url): static
    {
        return $this->fromType("UrlSchema")->urls($url);
    }

    /**
     * @param array $urls
     * @param string $library
     * @return $this
     */
    public function fromPlugin(array $urls, string $library): static
    {
        return $this->fromType("Plugin")->urls(implode(",",$urls))->library($library);
    }

    /**
     * 从物料市场安装
     * @param string $id
     * @param string $version
     * @param string $url
     * @return $this
     */
    public function market(string $id, string $version, string $url): static
    {
        return $this->setAttribute("market", compact('id','version','url'));
    }

    public function __call($method, $parameters)
    {
        return $this->toCall($method, $parameters);
    }
}
